<script setup lang="ts">
import { ref, reactive } from 'vue'
import { type AddLinkDto, type CapCertify } from '@/api/cap'
import { addLink, getCertifyList } from '@/api/cap'
import { type UserDto } from '@/api/sys'
import { getUserList } from '@/api/sys'
import { ElMessage } from 'element-plus'

const dialogForm = reactive<AddLinkDto>({})

const metaForm = reactive({
  name: '',
})

const clickSubDialogForm = () => {
  dialogForm.metaData = JSON.stringify(metaForm)
  addLink(dialogForm).then(() => {
    dialogVisible.value = false
    ElMessage({
      message: '操作成功！',
      type: 'success',
    })
  })
}

const userList = ref<UserDto[]>([])
const certifyList = ref<CapCertify[]>([])

const dialogVisible = ref(false)
const sourceId = ref('')
const openDialog = (par1: string) => {
  sourceId.value = par1
  dialogForm.sourceId = par1
  dialogVisible.value = true
  getCertifyList().then((res) => {
    certifyList.value = res
  })
  getUserList().then((res) => {
    userList.value = res
  })
}
defineExpose({
  openDialog,
})
</script>

<template>
  <el-dialog v-model="dialogVisible" title="发布链接" width="500">
    <el-form :model="dialogForm" label-width="auto" style="max-width: 600px">
      <el-form-item label="用户">
        <el-select v-model="dialogForm.userId" placeholder="please select">
          <el-option
            v-for="item in userList"
            :key="item.userId"
            :label="item.username"
            :value="item.userId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="凭证">
        <el-select v-model="dialogForm.certifyId" placeholder="please select">
          <el-option
            v-for="item in certifyList"
            :key="item.id"
            :label="item.description"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="名称">
        <el-input v-model="metaForm.name" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="clickSubDialogForm"> 提交 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped></style>
